package com.su.gulimall.order.listener;

import com.rabbitmq.client.Channel;
import com.su.gulimall.order.entity.OrderEntity;
import com.su.gulimall.order.service.OrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@RabbitListener(queues = "order.release.order.queue")
public class OrderCloseListener{

    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void listener(OrderEntity orderEntity, Channel channel, Message message) throws IOException {
    System.out.println("收到过期的订单信息，准备关闭订单"+orderEntity.getOrderSn());
    try {
        orderService.closeOrder(orderEntity);
        //TODO:订单关闭，库存解锁，此时手动调用支付宝收单,没写
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }catch (Exception e)
    {
        channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
    }

    }
}
